package bg.code.commons.builder.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/18
 * 描述：
 */
public class FileUtil {

    private static final Charset utf8 = Charset.forName("UTF-8");
    private static final int BUFFER = 1024;

    public FileUtil() {
    }

    public static String readStringByPlugin(String path) {
        StringBuilder results = new StringBuilder();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtil.class.getResourceAsStream(path), utf8));
            Throwable var3 = null;

            try {
                reader.lines().forEach((s) -> {
                    results.append(s).append("\n");
                });
            } catch (Throwable var13) {
                var3 = var13;
                throw var13;
            } finally {
                if (reader != null) {
                    if (var3 != null) {
                        try {
                            reader.close();
                        } catch (Throwable var12) {
                            var3.addSuppressed(var12);
                        }
                    } else {
                        reader.close();
                    }
                }

            }
        } catch (IOException var15) {
            var15.printStackTrace();
        }

        return results.toString();
    }

    public static String readStringByFile(File file) {
        StringBuilder results = new StringBuilder();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), utf8));
            Throwable var3 = null;

            try {
                reader.lines().forEach((s) -> {
                    results.append(s).append("\n");
                });
            } catch (Throwable var13) {
                var3 = var13;
                throw var13;
            } finally {
                if (reader != null) {
                    if (var3 != null) {
                        try {
                            reader.close();
                        } catch (Throwable var12) {
                            var3.addSuppressed(var12);
                        }
                    } else {
                        reader.close();
                    }
                }

            }
        } catch (IOException var15) {
            var15.printStackTrace();
        }

        return results.toString();
    }

    public static void strToFile(String fileContent, File file) {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                OutputStreamWriter osw = new OutputStreamWriter(fos, utf8);
                try {
                    osw.write(fileContent);
                    osw.flush();
                } catch (Throwable e) {
                    throw e;
                } finally {
                    if (osw != null) {
                        try {
                            osw.close();
                        } catch (Exception ex) {
                            throw ex;
                        }
                    } else {
                        osw.close();
                    }
                }
            } catch (Throwable e) {
                throw e;
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (Throwable e) {
                        throw e;
                    }
                } else {
                    fos.close();
                }
            }
        } catch (Exception e) {
        }
    }

    public static String ioToString(InputStream inputStream, String charsetName) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            result.write(buffer, 0, length);
        }

        return result.toString(charsetName);
    }

    public static void writeFile(File file, InputStream inputStream) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }

        write(inputStream, new FileOutputStream(file));
    }

    private static void write(InputStream input, OutputStream output) {
        try {
            byte[] buffer = new byte[1024];

            int bytesRead;
            while ((bytesRead = input.read(buffer, 0, 1024)) != -1) {
                output.write(buffer, 0, bytesRead);
            }

            output.flush();
        } catch (IOException var16) {
            var16.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException var15) {
                var15.printStackTrace();
            }

            try {
                output.close();
            } catch (IOException var14) {
                var14.printStackTrace();
            }

        }

    }

    public static void extractZipFile(File file, File root) {
        byte[] buffer = new byte[1024];

        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));

            while (true) {
                while (true) {
                    ZipEntry entry;
                    String fileName;
                    do {
                        if ((entry = zis.getNextEntry()) == null) {
                            zis.closeEntry();
                            zis.close();
                            return;
                        }

                        fileName = entry.getName();
                    } while (fileName.contains("__MACOSX"));

                    File newFile = new File(root.getAbsolutePath() + File.separator + fileName);
                    if (entry.isDirectory()) {
                        newFile.mkdirs();
                    } else {
                        newFile.getParentFile().mkdirs();
                        FileOutputStream fos = new FileOutputStream(newFile);

                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }

                        fos.close();
                    }
                }
            }
        } catch (IOException var9) {
            var9.printStackTrace();
        }
    }

    public static Path createTempFile() throws IOException {
        Path newFile;
        do {
            newFile = Paths.get(System.getProperty("java.io.tmpdir"), new String[]{"NutzFw", UUID.randomUUID().toString().replace("-", "") + ".tmp"});
        } while (newFile.toFile().exists());

        if (!newFile.getParent().toFile().exists()) {
            Files.createDirectory(newFile.getParent(), new FileAttribute[0]);
        }

        Files.createFile(newFile, new FileAttribute[0]);
        return newFile;
    }

    public static String createTempFolder(String folderName) throws IOException {
        return createTempFolderPath(folderName).toFile().toString();
    }

    public static Path createTempFolderPath(String folderName) throws IOException {
        Path newFile;
        do {
            newFile = Paths.get(System.getProperty("java.io.tmpdir"), new String[]{"NutzFw", UUID.randomUUID().toString().replace("-", ""), folderName});
        } while (newFile.toFile().exists());

        Files.createDirectories(newFile, new FileAttribute[0]);
        return newFile;
    }
}
